/***********************************************************************************************************************
    @file    flash_iap_dataspace.c
    @author  FAE Team
    @date    15-Mar-2023
    @brief   THIS FILE PROVIDES ALL THE SYSTEM FUNCTIONS.
  **********************************************************************************************************************
    @attention

    <h2><center>&copy; Copyright(c) <2023> <MindMotion></center></h2>

      Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
    following conditions are met:
    1. Redistributions of source code must retain the above copyright notice,
       this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
       the following disclaimer in the documentation and/or other materials provided with the distribution.
    3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or
       promote products derived from this software without specific prior written permission.

      THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *********************************************************************************************************************/

/* Define to prevent recursive inclusion */
#define _FLASH_IAP_DATASPACE_C_

/* Files include */
#include <stdio.h>
#include "platform.h"
#include "flash_dataspace.h"
#include "Control.h"

/**
 * @addtogroup MM32SPIN0230_LibSamples
 * @{
 */

/**
 * @addtogroup FLASH
 * @{
 */

/**
 * @addtogroup FLASH_IAP_DataSpace
 * @{
 */

/* Private typedef ****************************************************************************************************/

/* Private define *****************************************************************************************************/

/* Private macro ******************************************************************************************************/
#define IAP_DATASPACE_START_ADDRESS ((uint32_t)0x1FE00000)
#define IAP_DATASPACE_PAGE_SIZE (1024)

/* Private variables **************************************************************************************************/

/* Private functions **************************************************************************************************/

/***********************************************************************************************************************
 * @brief
 * @note   none
 * @param  none
 * @retval none
 *********************************************************************************************************************/
void FlashEEpromErase(void)
{
  uint32_t OffsetAddress = IAP_DATASPACE_START_ADDRESS;

  FLASH_Unlock();

  FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);
  FLASH_EraseDataAreaPage(OffsetAddress);
  FLASH_ClearFlag(FLASH_FLAG_EOP);
  FLASH_Lock();
}

/***********************************************************************************************************************
 * @brief
 * @note   none
 * @param  none
 * @retval none
 *********************************************************************************************************************/
void FlashEEpromWriteHalfWord(uint16_t Index, uint16_t Data)
{
  uint32_t OffsetAddress = 0;

  OffsetAddress = IAP_DATASPACE_START_ADDRESS + Index * 2;
  FLASH_Unlock();

  FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);
  FLASH_ProgramDataAreaHalfWord(OffsetAddress, Data);
  FLASH_ClearFlag(FLASH_FLAG_EOP);
  FLASH_Lock();
}

/***********************************************************************************************************************
 * @brief
 * @note   none
 * @param  none
 * @retval none
 *********************************************************************************************************************/
uint8_t FlashEEpromReadHalfWord(uint16_t Index)
{
  uint32_t Address = 0;
  uint16_t ReadValue = 0;
  Address = IAP_DATASPACE_START_ADDRESS + Index * 2;
  ReadValue = *(volatile uint16_t *)(Address);
  return (uint8_t)(ReadValue & 0xFF);
}

/***********************************************************************************************************************
 * @brief
 * @note   none
 * @param  none
 * @retval none
 *********************************************************************************************************************/
void FlashEEPROMCheck(void)
{
  uint8_t bInitEEPROM = 0; // dwl 1015 if eeprom test error init variable by default
  uint32_t Sum = 0;

  u8_Password_P0 = FlashEEpromReadHalfWord(E2prom_P0);
  Sum += u8_Password_P0;
  s8_SetTemp_C0 = (signed char)FlashEEpromReadHalfWord(E2prom_C0);
  Sum += (uint8_t)s8_SetTemp_C0;
  u8_TempUpReturnDiff_C1 = FlashEEpromReadHalfWord(E2prom_C1);
  Sum += u8_TempUpReturnDiff_C1;
  u8_TempDownReturnDiff_C2 = FlashEEpromReadHalfWord(E2prom_C2);
  Sum += u8_TempDownReturnDiff_C2;
  u8_CompressorOffTime_C3 = FlashEEpromReadHalfWord(E2prom_C3);
  Sum += u8_CompressorOffTime_C3;
  u8_Compressor1stOffTime_C4 = FlashEEpromReadHalfWord(E2prom_C4);
  Sum += u8_Compressor1stOffTime_C4;
  s8_SetTempUp_C5 = (signed char)FlashEEpromReadHalfWord(E2prom_C5);
  Sum += (uint8_t)s8_SetTempUp_C5;
  s8_SetTempDown_C6 = (signed char)FlashEEpromReadHalfWord(E2prom_C6);
  Sum += (uint8_t)s8_SetTempDown_C6;
  s8_TempComp_C7 = (signed char)FlashEEpromReadHalfWord(E2prom_C7);
  Sum += (uint8_t)s8_TempComp_C7;
  u8_SensorErrCompressorOnTime_C8 = FlashEEpromReadHalfWord(E2prom_C8);
  Sum += u8_SensorErrCompressorOnTime_C8;
  u8_SensorErrCompressorOffTime_C9 = FlashEEpromReadHalfWord(E2prom_C9);
  Sum += u8_SensorErrCompressorOffTime_C9;
  u8_DefrostCycle_D1 = FlashEEpromReadHalfWord(E2prom_D1);
  Sum += u8_DefrostCycle_D1;
  u8_DefrostTime_D2 = FlashEEpromReadHalfWord(E2prom_D2);
  Sum += u8_DefrostTime_D2;
  s8_HighTempErr_A1 = (signed char)FlashEEpromReadHalfWord(E2prom_A1);
  Sum += (uint8_t)s8_HighTempErr_A1;
  s8_LowTempErr_A2 = (signed char)FlashEEpromReadHalfWord(E2prom_A2);
  Sum += (uint8_t)s8_LowTempErr_A2;
  u8_AboveTempContinueTime_A3 = FlashEEpromReadHalfWord(E2prom_A3);
  Sum += u8_AboveTempContinueTime_A3;
  u8_DisplayMode_F1 = FlashEEpromReadHalfWord(E2prom_F1);
  Sum += u8_DisplayMode_F1;
  u8_DisplayMode_F2 = FlashEEpromReadHalfWord(E2prom_F2);
  Sum += u8_DisplayMode_F2;
  u8_DisplayTime_F3 = FlashEEpromReadHalfWord(E2prom_F3);
  Sum += u8_DisplayTime_F3;

  // Test EEPROM 1
  if ((0x55 != FlashEEpromReadHalfWord(E2prom_ID1)) && (0xAA != FlashEEpromReadHalfWord(E2prom_ID2)))
  {
    printf("\r\nEEPROM test1 failed!");
    bInitEEPROM = 1; // dwl 1015 if eeprom test error init variable by default
  }
  if ((Sum & 0xFF) != FlashEEpromReadHalfWord(E2prom_CheckSum))
  {
    printf("\r\nEEPROM test1 failed!");
    bInitEEPROM = 1; // dwl 1015 if eeprom test error init variable by default
  }
  if (bInitEEPROM == 1)
  {
    u8_Password_P0 = 55;
    s8_SetTemp_C0 = 3;
    u8_TempUpReturnDiff_C1 = 15;
    u8_TempDownReturnDiff_C2 = 15;
    u8_CompressorOffTime_C3 = 3;
    u8_Compressor1stOffTime_C4 = 0;
    s8_SetTempUp_C5 = 8;
    s8_SetTempDown_C6 = 2;
    s8_TempComp_C7 = -10;
    u8_SensorErrCompressorOnTime_C8 = 10;
    u8_SensorErrCompressorOffTime_C9 = 10;
    u8_DefrostCycle_D1 = 6;
    u8_DefrostTime_D2 = 25;
    s8_HighTempErr_A1 = 50;
    s8_LowTempErr_A2 = -40;
    u8_AboveTempContinueTime_A3 = 1;
    u8_DisplayMode_F1 = 0;
    u8_DisplayMode_F2 = 1;
    u8_DisplayTime_F3 = 0;

    FlashEEpromErase();
    FlashEEPROMSaveSetting();
  }
}

void FlashEEPROMSaveSetting(void)
{
  uint32_t Sum = 0;

  FlashEEpromErase();
  FlashEEpromWriteHalfWord(E2prom_P0, u8_Password_P0);
  Sum += u8_Password_P0;
  FlashEEpromWriteHalfWord(E2prom_C0, (uint8_t)s8_SetTemp_C0);
  Sum += (uint8_t)s8_SetTemp_C0;
  FlashEEpromWriteHalfWord(E2prom_C1, u8_TempUpReturnDiff_C1);
  Sum += u8_TempUpReturnDiff_C1;
  FlashEEpromWriteHalfWord(E2prom_C2, u8_TempDownReturnDiff_C2);
  Sum += u8_TempDownReturnDiff_C2;
  FlashEEpromWriteHalfWord(E2prom_C3, u8_CompressorOffTime_C3);
  Sum += u8_CompressorOffTime_C3;
  FlashEEpromWriteHalfWord(E2prom_C4, u8_Compressor1stOffTime_C4);
  Sum += u8_Compressor1stOffTime_C4;
  FlashEEpromWriteHalfWord(E2prom_C5, (uint8_t)s8_SetTempUp_C5);
  Sum += (uint8_t)s8_SetTempUp_C5;
  FlashEEpromWriteHalfWord(E2prom_C6, (uint8_t)s8_SetTempDown_C6);
  Sum += (uint8_t)s8_SetTempDown_C6;
  FlashEEpromWriteHalfWord(E2prom_C7, (uint8_t)s8_TempComp_C7);
  Sum += (uint8_t)s8_TempComp_C7;
  FlashEEpromWriteHalfWord(E2prom_C8, u8_SensorErrCompressorOnTime_C8);
  Sum += u8_SensorErrCompressorOnTime_C8;
  FlashEEpromWriteHalfWord(E2prom_C9, u8_SensorErrCompressorOffTime_C9);
  Sum += u8_SensorErrCompressorOffTime_C9;
  FlashEEpromWriteHalfWord(E2prom_D1, u8_DefrostCycle_D1);
  Sum += u8_DefrostCycle_D1;
  FlashEEpromWriteHalfWord(E2prom_D2, u8_DefrostTime_D2);
  Sum += u8_DefrostTime_D2;
  FlashEEpromWriteHalfWord(E2prom_A1, (uint8_t)s8_HighTempErr_A1);
  Sum += (uint8_t)s8_HighTempErr_A1;
  FlashEEpromWriteHalfWord(E2prom_A2, (uint8_t)s8_LowTempErr_A2);
  Sum += (uint8_t)s8_LowTempErr_A2;
  FlashEEpromWriteHalfWord(E2prom_A3, u8_AboveTempContinueTime_A3);
  Sum += u8_AboveTempContinueTime_A3;
  FlashEEpromWriteHalfWord(E2prom_F1, u8_DisplayMode_F1);
  Sum += u8_DisplayMode_F1;
  FlashEEpromWriteHalfWord(E2prom_F2, u8_DisplayMode_F2);
  Sum += u8_DisplayMode_F2;
  FlashEEpromWriteHalfWord(E2prom_F3, u8_DisplayTime_F3);
  Sum += u8_DisplayTime_F3;
  FlashEEpromWriteHalfWord(E2prom_CheckSum, (Sum & 0xFF));
  FlashEEpromWriteHalfWord(E2prom_ID1, 0x55);
  FlashEEpromWriteHalfWord(E2prom_ID2, 0xAA);
}
/********************************************************************
 * name         : CalcCheckSum
 * description  : 计算校验和
 * Input        :
 * Output       : none
 * Return       : none
 ********************************************************************/
// static uint8_t CalcCheckSum(const uint8_t *pData, uint32_t Len)
// {
//   uint32_t Sum = 0;
//   const uint8_t *pDataEnd = pData + Len;

//   while (pData < pDataEnd)
//   {
//     Sum += *pData++;
//   }

//   return (Sum & 0xFF);
// }

/**
 * @}
 */

/**
 * @}
 */

/**
 * @}
 */

/********************************************** (C) Copyright MindMotion **********************************************/
